package com.example.oauth.authserver.repository.entity;

import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * <p>
 *
 * </p>
 *
 * @author chang.zhou
 * @version 1$
 * Created in 2022/3/5 23:34
 */
@Getter
@Setter
@Entity
@Table(name = "auth_subject")
public class Subject {

    @Id
    @GenericGenerator(name = "snowflake", strategy = "com.example.oauth.authserver.common.SnowFlakeGenerator")
    @GeneratedValue(generator = "snowflake")
    private Long id;

    @Column(name = "login_name")
    private String loginName;

    private String password;

    @Column(name = "e_mail")
    private String eMail;

    @Column(name = "mobile_num")
    private String mobileNum;

    @Column(name = "secret_key")
    private String secretKey;

    @Column(name = "user_id")
    private Long userId;

    @Column(name = "account_not_expired")
    private Boolean accountNotExpired;

    @Column(name = "account_not_locked")
    private Boolean accountNotLocked;

    @Column(name = "credentials_not_expired")
    private Boolean credentialsNotExpired;

    private Boolean enabled;

    @Column(name = "create_time")
    private LocalDateTime createTime = LocalDateTime.now();

    @ManyToMany(targetEntity = Role.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    // name : 中间表名称
    @JoinTable(name = "auth_subject_role", //中间表名
            // name : 中间表存放本表字段的列名  referencedColumnName: 中间列存放本表的哪个字段
            joinColumns = {@JoinColumn(name = "subject_id",referencedColumnName = "id")},
            // name : 对方在中间表的列名，  referencedColumnName：对方在中间表的字段
            inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")})
    private Set<Role> roles = Sets.newHashSet();

}
